package com.gxuwz.endrskyweather;

import android.animation.ValueAnimator;
import android.content.Intent;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Shader;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextPaint;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;

import com.gxuwz.endrskyweather.service.DataSyncService;

public class MainActivity extends AppCompatActivity {
    private ValueAnimator colorAnimator;
    private TextView textViewTitle;
    private final int[] colors = { 0xFF9EFBD3, 0xFF57E9F2, 0xFF45D4FB };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 设置状态栏颜色
        getWindow().setStatusBarColor(getResources().getColor(R.color.black));
        // 隐藏ActionBar
        if (getSupportActionBar() != null) {
            getSupportActionBar().hide();
        }
        
        // 启动数据同步服务
        Intent serviceIntent = new Intent(this, DataSyncService.class);
        startService(serviceIntent);
        
        setContentView(R.layout.activity_main);

        // 获取 TextView
        textViewTitle = findViewById(R.id.textViewTitle);
        TextPaint textPaint = textViewTitle.getPaint();

        // 初始化动画
        initColorAnimation(textPaint);

        // 开始动画
        colorAnimator.start();

        // 2秒后跳转到主容器页面
        new Handler().postDelayed(() -> {
            Intent intent = new Intent(this, MainContainerActivity.class);
            startActivity(intent);
            finish(); // 关闭欢迎页面
        }, 3000);
    }

    private void initColorAnimation(TextPaint textPaint) {
        colorAnimator = ValueAnimator.ofFloat(0, 1);
        colorAnimator.setDuration(2000);
        colorAnimator.setRepeatCount(ValueAnimator.INFINITE);
        colorAnimator.setRepeatMode(ValueAnimator.RESTART);
        
        colorAnimator.addUpdateListener(animation -> {
            float fraction = animation.getAnimatedFraction();
            int width = textViewTitle.getWidth();
            if (width == 0) {
                width = (int) textPaint.measureText(textViewTitle.getText().toString());
            }
            
            // 扩展颜色数组和位置数组实现平滑过渡
            int[] extendedColors = {
                0xFF9EFBD3,  // 青色
                0xFF7BF4DD,  // 青蓝过渡色
                0xFF57E9F2,  // 蓝色
                0xFF45D4FB   // 深蓝
            };
            
            // 计算颜色位置
            float[] positions = new float[4];
            positions[0] = fraction;  // 青色位置
            positions[1] = (fraction + 0.2f) % 1.0f;  // 过渡色位置
            positions[2] = (fraction + 0.5f) % 1.0f;  // 蓝色位置
            positions[3] = (fraction + 1.0f) % 1.0f;  // 深蓝位置
            
            // 确保位置数组有序
            java.util.Arrays.sort(positions);
            
            Shader shader = new LinearGradient(
                    0, 0, width, textViewTitle.getTextSize(),
                    extendedColors, positions, Shader.TileMode.CLAMP
            );
            textPaint.setShader(shader);
            textViewTitle.invalidate();
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (colorAnimator != null) {
            colorAnimator.cancel();
        }
    }
}
